﻿using BLL;
using Models;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web
{
    public partial class GoodsDesc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                repeaterAdd();
                repeaterImgAdd();
            }
        }
        void repeaterAdd()
        {
            if (Request.QueryString["GoodsID"] != null)
            {
                GoodsBLL gb = new GoodsBLL();
                List<GoodsModels> list = gb.selectByID(int.Parse(Request.QueryString["GoodsID"].ToString()));
                Repeater1.DataSource = list;
                Repeater1.DataBind();
            }
            else
            {
                Response.Write("<script>alert('操作出错！')</script>");
            }

        }
        void repeaterImgAdd()
        {
            if (Request.QueryString["GoodsID"] != null)
            {
                GoodsDescBLL gd = new GoodsDescBLL();
                List<GoodsDescModels> list = gd.selectByID(int.Parse(Request.QueryString["GoodsID"].ToString()));
                Repeater2.DataSource = list;
                Repeater2.DataBind();
            }
            else
            {
                Response.Write("<script>alert('操作出错！')</script>");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)//加入购物车按钮
        {
            int nums = int.Parse(Request["ipt1"].ToString());
            int GoodsID = Convert.ToInt32(((Button)sender).CommandArgument);
            List<CartItem> list = CartItemBBL.selectID(GoodsID);


            List<CartItem> list1 = new List<CartItem>();
            CartItem um = new CartItem();
            um.GoodsName = list[0].GoodsName;
            um.GoodsPic = list[0].GoodsPic;
            um.GoodsPrice = list[0].GoodsPrice;
            um.GoodsID = GoodsID;
            um.Nums = nums;
            um.LittlePrice = nums * list[0].GoodsPrice;
            if (Request.Cookies["Cart"] != null)
            {
                string Cart = Request.Cookies["Cart"].Value;
                list1 = JsonConvert.DeserializeObject<List<CartItem>>(Cart);
                // 执行其他操作
            }
            bool isExisting = false;
            for (int i = 0; i < list1.Count; i++)
            {
                if (list1[i].GoodsID == um.GoodsID)
                {
                    list1[i].Nums += nums;
                    isExisting = true;
                    break;
                }
            }

            if (!isExisting)
            {
                list1.Add(um);
            }

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string json = serializer.Serialize(list1);
            HttpCookie httpCookie = new HttpCookie("Cart", json);
            httpCookie.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(httpCookie);
            Response.Redirect("UserCart.aspx");
        }
    }
}